import random
import os


if __name__ == '__main__':
    SHUFFLE = os.path.isfile('/var/tmp/SHUFFLE')
    with open('/var/tmp/playlist', 'r') as f:
        tracks = f.readlines()
        if len(tracks) == 0:
            exit(0)
        if SHUFFLE:
            track = random.choice(tracks)
        else:
            track = tracks[0]
        tracks.remove(track)
        print(track.strip(), end='')
    with open('/var/tmp/playlist', 'w') as f:
        for t in tracks:
            f.write(t)
    
