summaryrefslogtreecommitdiff
path: root/ar/.local/bin/browserprofile
blob: c66f183c29f1e2c14e7e4356480f551cd5c8c546 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/sh

# Define the profile paths
work_profile="$USER.work"
home_profile="$USER.default"
tmux_profile="$USER.tmux"

usage() {
  echo "Update the default profile in profiles.ini for Firefox or Librewolf."
  echo ""
  echo "Usage: ${0##*/} <browser> [<profile_type>]"
  echo ""
  echo "Options:"
  echo "  -h              : Show this help message."
  echo "  <browser>       : The browser to configure."
  echo "                    Accepted values:"
  echo "                      -f/firefox"
  echo "                      -l/librewolf"
  echo "  <profile_type>  : (Optional) If not specified, the default profile will be used."
  echo "                    Accepted values:"
  echo "                      work: Sets the work profile ($work_profile)"
  echo "                      default: Sets the home profile ($home_profile)"
  echo "                      tmux: Sets the home profile ($tmux_profile)"
  echo ""
  echo "Examples:"
  echo "  ${0##*/} -f -w    # Set the work profile for Firefox"
  echo "  ${0##*/} -l -d    # Set the default profile for Librewolf"
  echo "  ${0##*/} -f -d    # Set the default profile for Firefox"
  echo "  ${0##*/} -f -t    # Set the tmux profile for Firefox"
}

update_profiles_ini() {
  case "$1" in
  -f | --firefox | firefox) profiles_ini_path="$HOME/.mozilla/firefox/profiles.ini" ;;
  -l | --librewolf | librewolf) profiles_ini_path="$HOME/.librewolf/profiles.ini" ;;
  *)
    echo "Invalid browser type. Please use 'firefox' or 'librewolf'."
    return 1
    ;;
  esac

  profile_to_set=$2

  # Backup current profiles.ini
  cp "$profiles_ini_path" "$profiles_ini_path.bak"

  # Update the profiles.ini
  awk -v profile="$profile_to_set" '
    /^\[Install/ {
        print
        found=1
        next
    }
    found && /^Default=/ {
        sub(/=.*/, "=" profile)
        print
        next
    }
    {
        print
    }' "$profiles_ini_path" >"$profiles_ini_path.tmp" && mv "$profiles_ini_path.tmp" "$profiles_ini_path"

  echo "Updated profiles.ini to use profile: $profile_to_set"
}

# Main function
update_profile() {
  browser=$1
  profile_type=$2

  # Check if a browser is provided
  if [ -z "$browser" ]; then
    usage && exit 1
  fi

  # Convert profile_type to lowercase for case-insensitive comparison
  if [ -n "$profile_type" ]; then
    profile_type=$(echo "$profile_type" | tr '[:upper:]' '[:lower:]')
  else
    # Set to "default" if profile_type is not given
    profile_type="default"
  fi

  # Set the profile based on the input
  case "$profile_type" in
  -w | --work | work)
    update_profiles_ini "$browser" "$work_profile"
    ;;
  -d | --default | default)
    update_profiles_ini "$browser" "$home_profile"
    ;;
  -t | --tmux | tmux)
    update_profiles_ini "$browser" "$tmux_profile"
    ;;
  *)
    echo "Invalid profile type. Please use 'work' or 'default'."
    return 1
    ;;
  esac
}

# Check for help flag
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
  usage && exit 0
fi

# Execute the main function with arguments passed to the script
update_profile "$1" "$2"